#ifndef AUDIOTRANSMITTER_H
#define AUDIOTRANSMITTER_H

#include <QObject>
#include <QTcpSocket>
#include <QTimer>
#include <QMutex>
#include <QQueue>
#include <QByteArray>

class AudioTransmitter : public QObject
{
    Q_OBJECT

public:
    explicit AudioTransmitter(QObject *parent = nullptr);
    ~AudioTransmitter();

    // 连接管理
    bool connectToServer(const QString &host = "localhost", quint16 port = 8766);
    void disconnectFromServer();
    bool isConnected() const;

    // 音频数据发送
    void sendAudioData(const QByteArray &audioData);
    void setTransmissionEnabled(bool enabled);
    bool isTransmissionEnabled() const;

    // 配置
    void setBufferSize(int size);
    void setTransmissionInterval(int intervalMs);
    void setCompressionEnabled(bool enabled);

signals:
    void connectionStatusChanged(bool connected);
    void transmissionStatusChanged(bool enabled);
    void error(const QString &error);
    void dataSent(qint64 bytesSent);
    void connectionEstablished();
    void connectionLost();

private slots:
    void onSocketConnected();
    void onSocketDisconnected();
    void onSocketError(QAbstractSocket::SocketError error);
    void onTransmissionTimer();

private:
    void setupTransmissionTimer();
    void processAudioQueue();
    void sendDataPacket(const QByteArray &data);
    QByteArray compressAudioData(const QByteArray &audioData);

    // 网络相关
    QTcpSocket *m_socket;
    QString m_host;
    quint16 m_port;
    bool m_isConnected;

    // 传输控制
    bool m_transmissionEnabled;
    QTimer *m_transmissionTimer;
    int m_transmissionInterval;

    // 音频数据缓冲
    QQueue<QByteArray> m_audioQueue;
    QMutex m_queueMutex;
    int m_maxBufferSize;

    // 压缩设置
    bool m_compressionEnabled;

    // 统计信息
    qint64 m_totalBytesSent;
    qint64 m_packetsSent;
};

#endif // AUDIOTRANSMITTER_H
